﻿package org.kolonitsky.alexey.banner
{
    import flash.display.Sprite;

    import org.kolonitsky.alexey.flashui.ToggleButton;

    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.navigateToURL;

    import org.kolonitsky.alexey.media.VideoStream;
    import org.kolonitsky.alexey.media.VideoStreamEvent;

    /**
     *  Base class for banner with video content.
     */
    dynamic public class VideoBanner extends MovieClip
    {
        public var mcVideo:Sprite;
        public var btnPlay:ToggleButton;
        public var btnSound:ToggleButton;
        public var btn:Sprite;

        private var vs:VideoStream;
    
        public var videoURL:String = "";
        public var videoWidth:Number = 100;
        public var videoHeight:Number = 100;
        private var scr_stat:String = "";
        private var banner_stat:String = "";
        public var bannerLink:String = "http://preved.tut.by/?clickid=7601";

        public var skin:IVideoPlayerSkin;


        public function VideoBanner()
        {
            trace("INF: VideoBanner create v0.0.2");
            stage.showDefaultContextMenu = false;
            getExternalParams();
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
        }


        /** When banner loaded init it **/
        protected function addedToStageHandler (event:Event): void
        {
            trace("INFO: VideoBanner addedToStageHandler");
            createVideoStream(videoURL);
            prepareButtons();
        }


        /** Get flashvars parameters **/
        private function getExternalParams (): void
        {
            var params: Object = root.loaderInfo.parameters;
            if (params.videourl != undefined && params.videourl)
                videoURL = params.videourl;
            if (params.video_height != undefined && params.video_height)
                videoHeight = params.video_height;
            if (params.video_width != undefined && params.video_width)
                videoWidth = params.video_width;
            if (params.scr_stat != undefined && params.scr_stat != null)
                scr_stat = params.scr_stat;
            if (params.banner_stat != undefined && params.banner_stat != null)
                banner_stat = params.banner_stat;
        }


        /**  **/
        private function createVideoStream (url:String): void
        {

            mcVideo = skin.getVideoContainer();
         
            vs = new VideoStream (videoURL, videoWidth, videoHeight);

            // vivsdeo stream
            vs.addEventListener(VideoStreamEvent.STOP,
                function (event:VideoStreamEvent): void {
                btnPlay.selected = false;
                btnPlay.updateState(ToggleButton.STATE_UP)});
            vs.addEventListener(VideoStreamEvent.PLAY,
                function (event:VideoStreamEvent): void {
                btnPlay.selected = true;
                btnPlay.updateState(ToggleButton.STATE_UP_SELECTED)});
            vs.addEventListener(VideoStreamEvent.PAUSE,
                function (event:VideoStreamEvent): void {
                btnPlay.selected = false;
                btnPlay.updateState(ToggleButton.STATE_UP)});
            vs.addEventListener(VideoStreamEvent.MUTE,
                function (event:VideoStreamEvent): void {
                btnSound.selected = true;
                btnSound.updateState(ToggleButton.STATE_UP_SELECTED)});
            vs.addEventListener(VideoStreamEvent.UNMUTE,
                function (event:VideoStreamEvent): void {
                btnSound.selected = false;
                btnSound.updateState(ToggleButton.STATE_UP)});
         
            mcVideo.addChild(vs);
        }

        /**  **/
        private function prepareButtons (): void
        {
            // banner button
            btn = MovieClip(getChildByName("btn"));
            if (btn == null)
                btn = new MovieClip();
            btn.alpha = 0.0;
            btn.graphics.beginFill(0xFF0000, 0.0);
            btn.graphics.drawRect(0, 0, 240, 400);
            btn.graphics.endFill();
            btn.useHandCursor = true;
            btn.buttonMode = true;
            btn.addEventListener(MouseEvent.MOUSE_UP, onButtonClick);
         
            // play
            btnPlay = skin.getPlayButton();
            btnPlay.addEventListener(MouseEvent.MOUSE_UP, onPlayToggle);
         
            // sound
            btnSound = skin.getSoundButton();
            btnSound.addEventListener(MouseEvent.MOUSE_UP, onSoundToggle);
        }


        /**  **/
        private function onPlayToggle (event:MouseEvent): void
        {
            trace("INFO: VideoBanner onPlayToggle");
            if ((event.target as ToggleButton).selected) {
                var ldr:URLLoader = new URLLoader();
                ldr.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
                try {
                    ldr.load(new URLRequest(scr_stat));
                } catch (e:Error) {
                    trace("ERROR: VideoBanner " + e.message);
                }
                vs.play();
            } else {
                vs.stop();
            }
        }


        /**  **/
        private function onSoundToggle (event:MouseEvent): void
        {
            var btn: ToggleButton = event.target as ToggleButton;
            trace("INFO: VideoBanner onSoundToggle: " + btn.selected);
            vs.volume(btn.selected ? 0 : 50);
        }

    
        /**  **/
        private function onButtonClick (event:MouseEvent): void
        {
            var ldr:URLLoader = new URLLoader;
            ldr.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            ldr.addEventListener(Event.COMPLETE, onCompleteClick);
            try {
                ldr.load(new URLRequest(banner_stat));
            } catch (e:Error) {
                trace ("ERROR: " + e.message);
            }
            navigateToURL(new URLRequest(bannerLink), "_blank");
        }


        /**  **/
        private function onIOError (event:IOErrorEvent): void
        {
            trace("ERROR: VideoBanner: IOErr - " + event.text);
        }

        /**  **/
        private function onCompleteClick (event:Event): void
        {
            trace("ERROR: VideoBanner: CLICK_COMPLETE");
        }
    }
}
